package site.xuxingcan.Util;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import site.xuxingcan.Model.CardModel;
import site.xuxingcan.Model.OperationModel;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/**
 * Created by xuxingcan on 2016/7/20.
 */
public class CheckWinUtil {
    private static Gson gson = new Gson();

    public static void checkWin(ArrayList<CardModel> cardModelArrayList) {
        ArrayList<CardModel> doubleCardModel = new ArrayList<>();
        for (int i = 0; i < cardModelArrayList.size(); i++) {
            CardModel cardModel = cardModelArrayList.get(i);
            for (int j = i + 1; j < cardModelArrayList.size(); j++) {
                if (cardModel.equals(cardModelArrayList.get(j))) {
                    if (!doubleCardModel.contains(cardModel)) {
                        doubleCardModel.add(cardModel);
                    }
                    break;
                }
            }
        }
        Collections.sort(cardModelArrayList);
        for (CardModel cardModel : doubleCardModel) {
            ArrayList<CardModel> tempCardModels = (ArrayList<CardModel>) cardModelArrayList.clone();
            int times = 0;
            if (tempCardModels.remove(cardModel)) {
                times++;
            }
            if (tempCardModels.remove(cardModel)) {
                times++;
            }
            if (times == 2) {
                Log.d(cardModel.getNumber() + "");
                OperationModel operationModel = new OperationModel();
                long statTime = System.currentTimeMillis();
                Log.d(CheckWinUtil.checkSan(operationModel, tempCardModels) + "   " + operationModel.getSame() + "   " + operationModel.getShunzi() + "   " + operationModel.getOperationTimes());
                Log.d("time:" + (System.currentTimeMillis() - statTime));
            }
        }
    }

    /**
     * 同种花色检查（不是必须，但可以节省时间）
     *
     * @param cardModelArrayList
     */
    public static boolean checkSan(OperationModel operationModel, ArrayList<CardModel> cardModelArrayList) {
        operationModel.addOperationTimes();
        int size = cardModelArrayList.size();
        if (size % 3 != 0) {
            return false;
        }
        if (size == 0) {
            return true;
        }
//        cardModelArrayList = (ArrayList<CardModel>) cardModelArrayList.clone();
//        cardModelArrayList = deepCopyArrayList(cardModelArrayList);
        for (int i = 0; i < size; i++) {
            CardModel first = cardModelArrayList.get(i);
            for (int j = i + 1; j < size; j++) {
                CardModel second = cardModelArrayList.get(j);
                for (int k = j + 1; k < size; k++) {
                    CardModel third = cardModelArrayList.get(k);
                    if (first.equals(second) && second.equals(third)) {
                        operationModel.addSame();
                        ArrayList<CardModel> tempCardModels = (ArrayList<CardModel>) cardModelArrayList.clone();
                        tempCardModels.remove(first);
                        tempCardModels.remove(second);
                        tempCardModels.remove(third);
                        if (checkSan(operationModel, tempCardModels)) {
                            return true;
                        } else {
                            operationModel.reduceSame();
                        }
                    } else if (isShunzi(first, second, third)) {
                        operationModel.addShunzi();
                        ArrayList<CardModel> tempCardModels = (ArrayList<CardModel>) cardModelArrayList.clone();
                        tempCardModels.remove(first);
                        tempCardModels.remove(second);
                        tempCardModels.remove(third);
                        if (checkSan(operationModel, tempCardModels)) {
                            return true;
                        } else {
                            operationModel.reduceShunzi();
                        }
                    }
                }
            }
        }
        return false;
    }

    private static boolean isShunzi(CardModel first, CardModel second, CardModel third) {
        //123
        if (first.smallerThanTheGiven(second) && second.smallerThanTheGiven(third)) {
            return true;
        }
        //132
        if (first.smallerThanTheGiven(third) && second.biggerThanTheGiven(third)) {
            return true;
        }
        //213
        if (first.biggerThanTheGiven(second) && first.smallerThanTheGiven(third)) {
            return true;
        }
        //231
        if (first.smallerThanTheGiven(third) && first.biggerThanTheGiven(third)) {
            return true;
        }
        //312
        if (first.biggerThanTheGiven(third) && second.smallerThanTheGiven(third)) {
            return true;
        }
        //321
        if (first.biggerThanTheGiven(second) && second.biggerThanTheGiven(third)) {
            return true;
        }
        return false;
    }

    public static ArrayList<CardModel> deepCopyArrayList(ArrayList<CardModel> list) {
        try {
            String json = gson.toJson(list);
            return gson.fromJson(json,
                    new TypeToken<ArrayList<CardModel>>() {
                    }.getType());
        } catch (Exception e) {
            return new ArrayList<>();
        }
    }
}
